<?php

class Util{
        /**
         * getClinetIPAddress get client ip address
         * @return string
         */
        public static function getClinetIPAddress() {
            foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) {
                if (array_key_exists($key, $_SERVER) === true) {
                    foreach (explode(',', $_SERVER[$key]) as $ip) {
                        if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
                            return $ip;
                        }
                    }
                }
            }
            return "";
        }

        /*
         * getUniqueId generate unique id
         */
        public static function getUniqueId() {
            $s = strtoupper(md5(uniqid(rand(), true)));
            $guidText =
                    substr($s, 0, 8) . '-' .
                    substr($s, 8, 4) . '-' .
                    substr($s, 12, 4) . '-' .
                    substr($s, 16, 4) . '-' .
                    substr($s, 20);
            return $guidText;
        }
        
public static function fnEncrypt($text, $key, $initialVector)
{
    return rtrim(
        base64_encode(
            mcrypt_encrypt(
                MCRYPT_RIJNDAEL_128,
                $key, $text, 
                MCRYPT_MODE_CBC, 
                $initialVector
                )
            ), "\0"
        );
}

public static function fnDecrypt($sValue, $sSecretKey, $initialVector)
{
    return rtrim(
        mcrypt_decrypt(
            MCRYPT_RIJNDAEL_128, 
            $sSecretKey, 
            base64_decode($sValue), 
            MCRYPT_MODE_CBC,
            $initialVector
        ), "\0"
    );
}



}

?>
